{% extends "base.html" %}
{% load iptocountry_flag %}
{% block content %}
<h1>{% get_flag poll.ip_address as flag %} {{ poll.title }}</h1>
<h2>Poll created by {{ poll.user }}, the {{ poll.pub_date|date:"d M Y" }}</h2>
<h3>{{ poll.description }}</h3>
{% if not my_vote %}
<ul id="choices">
{% for choice in poll.choice_set.all %}
    <li><a href="{% url poll_vote poll.pk choice.pk %}">{{ choice.choice }}</a></li>
{% endfor %}
</ul>
{% else %}
<ul>
{% for choice,nb in votes.items %}
    <li>{{ choice }}: {{ nb }}</li>
{% endfor %}
</ul>
<p>Your vote to this poll is <strong>'{{ my_vote.0 }}'</strong></p>
{% endif %}
{% endblock %}

{% block code %}
<span class="k">class</span> <span class="nc">ViewPoll</span><span class="p">(</span><span class="n">View</span><span class="p">):</span>
    <span class="n">template_name</span> <span class="o">=</span> <span class="s">&quot;polls/poll_view.html&quot;</span>

    <span class="k">def</span> <span class="nf">build_params</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">poll_id</span><span class="p">):</span>
        <span class="n">poll</span> <span class="o">=</span> <span class="n">get_object_or_404</span><span class="p">(</span><span class="n">Poll</span><span class="p">,</span> <span class="n">pk</span><span class="o">=</span><span class="n">poll_id</span><span class="p">)</span>
        <span class="n">votes</span> <span class="o">=</span> <span class="n">Vote</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">poll</span><span class="o">=</span><span class="n">poll</span><span class="p">,</span> <span class="n">user</span><span class="o">=</span><span class="n">request</span><span class="o">.</span><span class="n">user</span><span class="p">)</span>
        <span class="k">return</span> <span class="p">{</span><span class="s">&#39;poll&#39;</span><span class="p">:</span> <span class="n">poll</span><span class="p">,</span>
                <span class="s">&#39;voted&#39;</span><span class="p">:</span> <span class="n">votes</span><span class="o">.</span><span class="n">count</span><span class="p">(),</span>
                <span class="s">&#39;votes&#39;</span><span class="p">:</span> <span class="n">votes</span><span class="p">}</span>
{% if not voted %}
<span class="k">class</span> <span class="nc">DoVote</span><span class="p">(</span><span class="n">View</span><span class="p">):</span>
    <span class="n">template_name</span> <span class="o">=</span> <span class="s">&quot;polls/poll_vote.html&quot;</span>
    <span class="n">login_required</span> <span class="o">=</span> <span class="bp">True</span>
    
    <span class="k">def</span> <span class="nf">build_params</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">poll_id</span><span class="p">,</span> <span class="n">choice_id</span><span class="p">):</span>
        <span class="n">user</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">user</span>
        <span class="n">poll</span> <span class="o">=</span> <span class="n">get_object_or_404</span><span class="p">(</span><span class="n">Poll</span><span class="p">,</span> <span class="n">pk</span><span class="o">=</span><span class="n">poll_id</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">user</span><span class="o">.</span><span class="n">is_authenticated</span><span class="p">()</span> <span class="ow">and</span> <span class="n">Vote</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span>
                 <span class="n">poll</span><span class="o">=</span><span class="n">poll</span><span class="p">,</span> <span class="n">user</span><span class="o">=</span><span class="n">user</span><span class="p">):</span>
            <span class="k">raise</span> <span class="n">CutResponse</span><span class="p">(</span><span class="n">HttpResponse</span><span class="p">(</span><span class="s">&#39;already voted&#39;</span><span class="p">))</span>
        <span class="n">choice</span> <span class="o">=</span> <span class="n">get_object_or_404</span><span class="p">(</span><span class="n">Choice</span><span class="p">,</span> <span class="n">pk</span><span class="o">=</span><span class="n">choice_id</span><span class="p">)</span>
        <span class="n">vote</span> <span class="o">=</span> <span class="n">Vote</span><span class="p">(</span><span class="n">user</span><span class="o">=</span><span class="n">user</span><span class="p">,</span> <span class="n">poll</span><span class="o">=</span><span class="n">poll</span><span class="p">,</span> <span class="n">choice</span><span class="o">=</span><span class="n">choice</span><span class="p">,</span>
                    <span class="n">ip_address</span><span class="o">=</span><span class="n">request</span><span class="o">.</span><span class="n">META</span><span class="p">[</span><span class="s">&#39;REMOTE_ADDR&#39;</span><span class="p">])</span>
        <span class="n">vote</span><span class="o">.</span><span class="n">save</span><span class="p">()</span>
        <span class="k">return</span> <span class="p">{</span><span class="s">&#39;redirect&#39;</span><span class="p">:</span> <span class="n">poll</span><span class="o">.</span><span class="n">get_absolute_url</span><span class="p">()}</span>
{% endif %}
{% endblock %}
